﻿using Dapper;
using Domain.Entity;
using Domain.Repository;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class CFTKRepository : ICFTKRepository
    {
        private readonly MyDbContext myDbContext;

        public CFTKRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<int> AddCFTK(CFTK cFTK)
        {
            myDbContext.CFTK.Add(cFTK);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> DeleteCFTK(int id)
        {
            var CFTK = await myDbContext.CFTK.FindAsync(id);
            if (CFTK != null)
            {
                myDbContext.CFTK.Remove(CFTK);
                return await myDbContext.SaveChangesAsync();
            }
            return 0;
        }

        public List<CFTK> GetCFTK()
        {
            using (SqlConnection connection = new SqlConnection("server=.;database=HR;user=sa;pwd=cssl#123;Encrypt=False;"))
            {
                string sql = "select * from dbo.CFTK  ";
                return connection.Query<CFTK>(sql).ToList();
            }
        }

		public List<CFTK> GetCFTKName(string name)
		{
			var zhi = myDbContext.CFTK.Where(e => e.SecondKindName == name).ToList();
			return zhi;
		}

		public async Task<int> UpdateCFTK(CFTK cFTK)
        {
            myDbContext.CFTK.Update(cFTK);
            int result=await myDbContext.SaveChangesAsync();
            return result;
            
        }
    }
}
